home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / PNGLIB06.ZIP / PNG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-01  |  4.8 KB  |  172 lines

  1.  
  2. /* png.c - location for general purpose png functions
  3.  
  4.    pnglib version 0.6
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
  7.    May 1, 1995
  8.    */
  9.  
  10. #define PNG_INTERNAL
  11. #define PNG_NO_EXTERN
  12. #include "png.h"
  13.  
  14. /* place to hold the signiture string for a png file. */
  15. png_byte png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  16.  
  17. /* constant strings for known chunk types.  If you need to add a chunk,
  18.    add a string holding the name here.  If you want to make the code
  19.    portable to EBCDIC machines, use ASCII numbers, not characters. */
  20. png_byte png_IHDR[4] = { 73,  72,  68,  82};
  21. png_byte png_IDAT[4] = { 73,  68,  65,  84};
  22. png_byte png_IEND[4] = { 73,  69,  78,  68};
  23. png_byte png_PLTE[4] = { 80,  76,  84,  69};
  24. png_byte png_gAMA[4] = {103,  65,  77,  65};
  25. png_byte png_sBIT[4] = {115,  66,  73,  84};
  26. png_byte png_cHRM[4] = { 99,  72,  82,  77};
  27. png_byte png_tRNS[4] = {116,  82,  78,  83};
  28. png_byte png_bKGD[4] = { 98,  75,  71,  68};
  29. png_byte png_hIST[4] = {104,  73,  83,  84};
  30. png_byte png_tEXt[4] = {116,  69,  88, 116};
  31. png_byte png_zTXt[4] = {122,  84,  88, 116};
  32. png_byte png_pHYs[4] = {112,  72,  89, 115};
  33. png_byte png_oFFs[4] = {111,  70,  70, 115};
  34. png_byte png_tIME[4] = {116,  73,  77,  69};
  35.  
  36. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  37.  
  38. /* start of interlace block */
  39. int png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  40.  
  41. /* offset to next interlace block */
  42. int png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  43.  
  44. /* start of interlace block in the y direction */
  45. int png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  46.  
  47. /* offset to next interlace block in the y direction */
  48. int png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  49.  
  50. /* width of interlace block */
  51. /* this is not currently used - if you need it, uncomment it here and
  52.    in png.h
  53. int png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
  54. */
  55.  
  56. /* height of interlace block */
  57. /* this is not currently used - if you need it, uncomment it here and
  58.    in png.h
  59. int png_pass_height[] = {8, 8, 4, 4, 4, 2, 2, 1};
  60. */
  61.  
  62. /* mask to determine which pixels are valid in a pass */
  63. int png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  64.  
  65. /* mask to determine which pixels to overwrite while displaying */
  66. int png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
  67.  
  68.  
  69. int png_check_sig(png_byte *sig, int num)
  70. {
  71.    if (num > 8)
  72.       num = 8;
  73.    if (num < 1)
  74.       return 0;
  75.  
  76.    return (!memcmp(sig, png_sig, num));
  77. }
  78.  
  79. /* Function to allocate memory for zlib. */
  80. voidp
  81. png_zalloc(voidp png_ptr, uInt items, uInt size)
  82. {
  83.    return ((voidp)png_large_malloc((png_struct *)png_ptr,
  84.       (png_uint_32)items * (png_uint_32)size));
  85. }
  86.  
  87. /* function to free memory for zlib */
  88. void
  89. png_zfree(voidp png_ptr, voidp ptr)
  90. {
  91.    png_large_free((png_struct *)png_ptr, (void *)ptr);
  92. }
  93.  
  94. /* reset the crc variable to 32 bits of 1's.  Care must be taken
  95.    in case crc is > 32 bits to leave the top bits 0 */
  96. void
  97. png_reset_crc(png_struct *png_ptr)
  98. {
  99.    /* set crc to all 1's */
  100.    png_ptr->crc = 0xffffffffL;
  101. }
  102.  
  103. /* Note: the crc code below was copied from the sample code in the
  104.    PNG spec, with appropriate modifications made to ensure the
  105.    variables are large enough */
  106.  
  107. /* table of crc's of all 8-bit messages.  If you wish to png_malloc this
  108.    table, turn this into a pointer, and png_malloc it in make_crc_table().
  109.    You may then want to hook it into png_struct and free it with the
  110.    destroy functions. */
  111. static png_uint_32 crc_table[256];
  112.  
  113. /* Flag: has the table been computed? Initially false. */
  114. static int crc_table_computed = 0;
  115.  
  116. /* make the table for a fast crc */
  117. static void
  118. make_crc_table(void)
  119. {
  120.   png_uint_32 c;
  121.   int n, k;
  122.  
  123.   for (n = 0; n < 256; n++)
  124.   {
  125.    c = (png_uint_32)n;
  126.    for (k = 0; k < 8; k++)
  127.      c = c & 1 ? 0xedb88320L ^ (c >> 1) : c >> 1;
  128.    crc_table[n] = c;
  129.   }
  130.   crc_table_computed = 1;
  131. }
  132.  
  133. /* update a running crc with the bytes buf[0..len-1]--the crc should be
  134.    initialized to all 1's, and the transmitted value is the 1's complement
  135.    of the final running crc. */
  136. static png_uint_32
  137. update_crc(png_uint_32 crc, png_byte *buf, png_uint_32 len)
  138. {
  139.   png_uint_32 c;
  140.   png_byte *p;
  141.   png_uint_32 n;
  142.  
  143.   c = crc;
  144.   p = buf;
  145.   n = len;
  146.  
  147.   if (!crc_table_computed)
  148.   {
  149.    make_crc_table();
  150.   }
  151.  
  152.   if (n > 0) do
  153.   {
  154.    c = crc_table[(png_byte)((c ^ (*p++)) & 0xff)] ^ (c >> 8);
  155.   } while (--n);
  156.  
  157.   return c;
  158. }
  159.  
  160. /* calculate the crc over a section of data.  Note that while we
  161.    are passing in a 32 bit value for length, on 16 bit machines, you
  162.    would need to use huge pointers to access all that data.  If you
  163.    need this, put huge here and above. */
  164. void
  165. png_calculate_crc(png_struct *png_ptr, png_byte *ptr,
  166.    png_uint_32 length)
  167. {
  168.    png_ptr->crc = update_crc(png_ptr->crc, ptr, length);
  169. }
  170.  
  171.  
  172.